home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2002 November / CD 1 / APC0211D1.ISO / workshop / prog / files / ActivePerl-5.6.1.633-MSWin32.msi / _74a8e861bb514236b0cc7f08d9571b5e < prev    next >
Encoding:
Text File  |  2002-05-01  |  7.8 KB  |  302 lines

  1. package LWP::ConnCache;
  2.  
  3. # $Id: ConnCache.pm,v 1.4 2001/04/20 20:24:42 gisle Exp $
  4.  
  5. use strict;
  6. use vars qw($VERSION $DEBUG);
  7.  
  8. $VERSION = "0.01";
  9.  
  10. sub new {
  11.     my($class, %cnf) = @_;
  12.     my $total_capacity = delete $cnf{total_capacity};
  13.     $total_capacity = 1 unless defined $total_capacity;
  14.     if (%cnf && $^W) {
  15.     require Carp;
  16.     Carp::carp("Unrecognised options: @{[sort keys %cnf]}")
  17.     }
  18.     my $self = bless { cc_conns => [] }, $class;
  19.     $self->total_capacity($total_capacity);
  20.     $self;
  21. }
  22.  
  23. sub deposit {
  24.     my($self, $type, $key, $conn) = @_;
  25.     push(@{$self->{cc_conns}}, [$conn, $type, $key, time]);
  26.     $self->enforce_limits($type);
  27.     return;
  28. }
  29.  
  30. sub withdraw {
  31.     my($self, $type, $key) = @_;
  32.     my $conns = $self->{cc_conns};
  33.     for my $i (0 .. @$conns - 1) {
  34.     my $c = $conns->[$i];
  35.     next unless $c->[1] eq $type && $c->[2] eq $key;
  36.     splice(@$conns, $i, 1);  # remove it
  37.     return $c->[0];
  38.     }
  39.     return undef;
  40. }
  41.  
  42. sub total_capacity {
  43.     my $self = shift;
  44.     my $old = $self->{cc_limit_total};
  45.     if (@_) {
  46.     $self->{cc_limit_total} = shift;
  47.     $self->enforce_limits;
  48.     }
  49.     $old;
  50. }
  51.  
  52. sub capacity {
  53.     my $self = shift;
  54.     my $type = shift;
  55.     my $old = $self->{cc_limit}{$type};
  56.     if (@_) {
  57.     $self->{cc_limit}{$type} = shift;
  58.     $self->enforce_limits($type);
  59.     }
  60.     $old;
  61. }
  62.  
  63. sub enforce_limits {
  64.     my($self, $type) = @_;
  65.     my $conns = $self->{cc_conns};
  66.  
  67.     my @types = $type ? ($type) : ($self->get_types);
  68.     for $type (@types) {
  69.     next unless $self->{cc_limit};
  70.     my $limit = $self->{cc_limit}{$type};
  71.     next unless defined $limit;
  72.     for my $i (reverse 0 .. @$conns - 1) {
  73.         next unless $conns->[$i][1] eq $type;
  74.         if (--$limit < 0) {
  75.         $self->dropping(splice(@$conns, $i, 1), "$type capacity exceeded");
  76.         }
  77.     }
  78.     }
  79.  
  80.     if (defined(my $total = $self->{cc_limit_total})) {
  81.     while (@$conns > $total) {
  82.         $self->dropping(shift(@$conns), "Total capacity exceeded");
  83.     }
  84.     }
  85. }
  86.  
  87. sub dropping {
  88.     my($self, $c, $reason) = @_;
  89.     print "DROPPING @$c [$reason]\n" if $DEBUG;
  90. }
  91.  
  92. sub drop {
  93.     my($self, $checker, $reason) = @_;
  94.     if (ref($checker) ne "CODE") {
  95.     # make it so
  96.     if (!defined $checker) {
  97.         $checker = sub { 1 };  # drop all of them
  98.     }
  99.     elsif (_looks_like_number($checker)) {
  100.         my $age_limit = $checker;
  101.         my $time_limit = time - $age_limit;
  102.         $reason ||= "older than $age_limit";
  103.         $checker = sub { $_[3] < $time_limit };
  104.     }
  105.     else {
  106.         my $type = $checker;
  107.         $reason ||= "drop $type";
  108.         $checker = sub { $_[1] eq $type };  # match on type
  109.     }
  110.     }
  111.     $reason ||= "drop";
  112.  
  113.     local $SIG{__DIE__};  # don't interfere with eval below
  114.     local $@;
  115.     my @c;
  116.     for (@{$self->{cc_conns}}) {
  117.     my $drop;
  118.     eval {
  119.         if (&$checker(@$_)) {
  120.         $self->dropping($_, $reason);
  121.         $drop++;
  122.         }
  123.     };
  124.     push(@c, $_) unless $drop;
  125.     }
  126.     @{$self->{cc_conns}} = @c;
  127. }
  128.  
  129. sub prune {
  130.     my $self = shift;
  131.     $self->drop(sub { !shift->ping }, "ping");
  132. }
  133.  
  134. sub get_types {
  135.     my $self = shift;
  136.     my %t;
  137.     $t{$_->[1]}++ for @{$self->{cc_conns}};
  138.     return keys %t;
  139. }
  140.  
  141. sub get_connections {
  142.     my($self, $type) = @_;
  143.     my @c;
  144.     for (@{$self->{cc_conns}}) {
  145.     push(@c, $_->[0]) if !$type || ($type && $type eq $_->[1]);
  146.     }
  147.     @c;
  148. }
  149.  
  150. sub _looks_like_number {
  151.     $_[0] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
  152. }
  153.  
  154. 1;
  155.  
  156. __END__
  157.  
  158. =head1 NAME
  159.  
  160. LWP::ConnCache - Connection cache manager
  161.  
  162. =head1 NOTE
  163.  
  164. This module is experimental.  Details of its interface is likely to
  165. change in the future.
  166.  
  167. =head1 SYNOPSIS
  168.  
  169.  use LWP::ConnCache;
  170.  my $cache = LWP::ConnCache->new;
  171.  $cache->deposit($type, $key, $sock);
  172.  $sock = $cache->withdraw($type, $key);
  173.  
  174. =head1 DESCRIPTION
  175.  
  176. The C<LWP::ConnCache> class is the standard connection cache manager
  177. for LWP::UserAgent.
  178.  
  179. The following basic methods are provided:
  180.  
  181. =over
  182.  
  183. =item $cache = LWP::ConnCache->new( %options )
  184.  
  185. This method constructs a new C<LWP::ConnCache> object.  The only
  186. option currently accepted is 'total_capacity'.  If specified it
  187. initalize the total_capacity option.  It defaults to the value 1.
  188.  
  189. =item $cache->total_capacity( [$num_connections] )
  190.  
  191. Get/sets the number of connection that will be cached.  Connections
  192. will start to be dropped when this limit is reached.  If set to C<0>,
  193. then all connections are immediately dropped.  If set to C<undef>,
  194. then there is no limit.
  195.  
  196. =item $cache->capacity($type, [$num_connections] )
  197.  
  198. Get/set a limit for the number of connections of the specifed type
  199. that can be cached.  The $type will typically be a short string like
  200. "http" or "ftp".
  201.  
  202. =item $cache->drop( [$checker, [$reason]] )
  203.  
  204. Drop connections by some criteria.  The $checker argument is a
  205. subroutine that is called for each connection.  If the routine returns
  206. a TRUE value then the connection is dropped.  The routine is called
  207. with ($conn, $type, $key, $deposit_time) as arguments.
  208.  
  209. Shortcuts: If the $checker argument is absent (or C<undef>) all cached
  210. connections are dropped.  If the $checker is a number then all
  211. connections untouched that the given number of seconds or more are
  212. dropped.  If $checker is a string then all connections of the given
  213. type are dropped.
  214.  
  215. The $reason argument is passed on to the dropped() method.
  216.  
  217. =item $cache->prune
  218.  
  219. Calling this method will drop all connections that are dead.  This is
  220. tested by calling the ping() method on the connections.  If the ping()
  221. method exists and returns a FALSE value, then the connection is
  222. dropped.
  223.  
  224. =item $cache->get_types
  225.  
  226. This returns all the 'type' fields used for the currently cached
  227. connections.
  228.  
  229. =item $cache->get_connections( [$type] )
  230.  
  231. This returns all connection objects of the specified type.  If no type
  232. is specified then all connections are returned.  In scalar context the
  233. number of cached connections of the specified type is returned.
  234.  
  235. =back
  236.  
  237.  
  238. The following methods are called by low-level protocol modules to
  239. try to save away connections and to get them back.
  240.  
  241. =over
  242.  
  243. =item $cache->deposit($type, $key, $conn)
  244.  
  245. This method adds a new connection to the cache.  As a result other
  246. already cached connections might be dropped.  Multiple connections with
  247. the same $type/$key might added.
  248.  
  249. =item $conn = $cache->withdraw($type, $key)
  250.  
  251. This method tries to fetch back a connection that was previously
  252. deposited.  If no cached connection with the specified $type/$key is
  253. found, then C<undef> is returned.  There is not guarantee that a
  254. deposited connection can be withdrawn, as the cache manger is free to
  255. drop connections at any time.
  256.  
  257. =back
  258.  
  259. The following methods are called internally.  Subclasses might want to
  260. override them.
  261.  
  262. =over
  263.  
  264. =item $conn->enforce_limits([$type])
  265.  
  266. This method is called with after a new connection is added (deposited)
  267. in the cache or capacity limits are adjusted.  The default
  268. implementation drops connections until the specified capacity limits
  269. are not exceeded.
  270.  
  271. =item $conn->dropping($conn_record, $reason)
  272.  
  273. This method is called when a connection is dropped.  The record
  274. belonging to the dropped connection is passed as the first argument
  275. and a string describing the reason for the drop is passed as the
  276. second argument.  The default implementation makes some noise if the
  277. $LWP::ConnCache::DEBUG variable is set and nothing more.
  278.  
  279. =back
  280.  
  281. =head1 SUBCLASSING
  282.  
  283. For specialized cache policy it makes sense to subclass
  284. C<LWP::ConnCache> and perhaps override the deposit(), enforce_limits()
  285. and dropping() methods.
  286.  
  287. The object itself is a hash.  Keys prefixed with C<cc_> are reserved
  288. for the base class.
  289.  
  290. =head1 SEE ALSO
  291.  
  292. L<LWP::UserAgent>
  293.  
  294. =head1 COPYRIGHT
  295.  
  296. Copyright 2001 Gisle Aas.
  297.  
  298. This library is free software; you can redistribute it and/or
  299. modify it under the same terms as Perl itself.
  300.  
  301. =cut
  302.